package cn.csust.net2.manager.client.panel;

import java.util.ArrayList;
import java.util.List;

import cn.csust.net2.manager.client.Interaction.NoticeWriteWindow;
import cn.csust.net2.manager.client.app.App;
import cn.csust.net2.manager.client.resources.Resources;
import cn.csust.net2.manager.client.ux.BeanGrid;
import cn.csust.net2.manager.client.ux.BeanGridPanel;
import cn.csust.net2.manager.shared.po.Notice;
import cn.csust.net2.manager.shared.po.PO;
import cn.csust.net2.manager.shared.service.NoticeManagerService;
import cn.csust.net2.manager.shared.service.NoticeManagerServiceAsync;
import cn.csust.net2.manager.shared.vo.Config;

import com.extjs.gxt.ui.client.Style.LayoutRegion;
import com.extjs.gxt.ui.client.binding.FormBinding;
import com.extjs.gxt.ui.client.data.BeanModel;
import com.extjs.gxt.ui.client.data.ModelData;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.Events;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.event.MessageBoxEvent;
import com.extjs.gxt.ui.client.event.SelectionChangedEvent;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.util.Margins;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.Info;
import com.extjs.gxt.ui.client.widget.MessageBox;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.form.DateField;
import com.extjs.gxt.ui.client.widget.form.FieldSet;
import com.extjs.gxt.ui.client.widget.form.FormPanel;
import com.extjs.gxt.ui.client.widget.form.HtmlEditor;
import com.extjs.gxt.ui.client.widget.form.TextField;
import com.extjs.gxt.ui.client.widget.layout.BorderLayout;
import com.extjs.gxt.ui.client.widget.layout.BorderLayoutData;
import com.extjs.gxt.ui.client.widget.layout.CardLayout;
import com.extjs.gxt.ui.client.widget.layout.FitLayout;
import com.extjs.gxt.ui.client.widget.layout.FormData;
import com.extjs.gxt.ui.client.widget.toolbar.SeparatorToolItem;
import com.extjs.gxt.ui.client.widget.toolbar.ToolBar;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.AbstractImagePrototype;
/**
 * 
 * 公告管理panel
 * @author Administrator
 *
 */
public class NoticeManagePanel   extends ContentPanel {

	

	private NoticeManagerServiceAsync noticeManagerService;
	private ContentPanel contentPanel;
	
	
	private BeanGridPanel<Notice> noticeGridPanel;
	
	//公告grid
	private BeanGrid<Notice> grid;
	//公告查看panel
	private FormPanel checkPanel;
	
	private FormBinding formBindings;

	
	
	TextField<String> titleFiled;
	TextField<String> senderFiled;
	DateField startDateFiled ;
	DateField endDateFiled;
	HtmlEditor content ;
	
	//查看公告和发布公告panel切换
	private CardLayout cardLayout;
	
	
	public NoticeManagePanel() {

		setHeaderVisible(false);
		cardLayout = new CardLayout();
		this.setLayout(cardLayout);
		init();
	}
	
	private void init(){
		noticeManagerService = GWT.create(NoticeManagerService.class);	
		createContentPanel();
		

	}
	
	private void createContentPanel(){
		
		contentPanel = new ContentPanel();
		contentPanel.setHeaderVisible(false);
		contentPanel.setLayout(new BorderLayout());   
		
		
		ToolBar toolBar = new ToolBar();
		Button publishButton = new Button("发布");
		publishButton.setIcon(AbstractImagePrototype.create(Resources.ICONS.publish()));
		publishButton.addSelectionListener(new SelectionListener<ButtonEvent>(){

			@Override
			public void componentSelected(ButtonEvent ce) {
				// TODO Auto-generated method stub
				NoticeWriteWindow nww = new NoticeWriteWindow();
				nww.show();
				nww.noticeManagePanel = NoticeManagePanel.this;
			}	    	
	    });
		Button deleteButton = new Button("删除", AbstractImagePrototype.create(Resources.ICONS.toolBarDelete()));
		deleteButton.addSelectionListener(new SelectionListener<ButtonEvent>(){

			@Override
			public void componentSelected(ButtonEvent ce) {
				// TODO Auto-generated method stub
				delete();
			}
			
		});
		toolBar.add(publishButton);
		toolBar.add(new SeparatorToolItem());
		toolBar.add(deleteButton);	
		contentPanel.setTopComponent(toolBar);
		
		createNoticeGridPanel();
		createCheckPanel();
		 
		this.add(contentPanel);
	}
	
	
	//左侧表格
	private void createNoticeGridPanel(){	
		noticeGridPanel = new BeanGridPanel<Notice>(new Notice(),noticeManagerService);
		noticeGridPanel.setAutoLoad(true);
		noticeGridPanel.createGrid();
		noticeGridPanel.createPagingToolBar();
		
		grid = noticeGridPanel.getGrid();
		
		noticeGridPanel.getGrid().getSelectionModel().addListener(Events.SelectionChange,
				new Listener<SelectionChangedEvent<BeanModel>>() {
			public void handleEvent(
					SelectionChangedEvent<BeanModel> be) {
				if (be.getSelection().size() > 0) {
					formBindings.bind((ModelData) be
							.getSelection().get(0));
				} else {
					formBindings.unbind();
				}
			}
		});

		
		FieldSet fieldSet = new FieldSet();   
		fieldSet.setHeading("公告");
		fieldSet.setLayout(new FitLayout());
		fieldSet.add(noticeGridPanel);
		BorderLayoutData centerData = new BorderLayoutData(LayoutRegion.CENTER);
		centerData.setMargins(new Margins(10,10,10,10));
		contentPanel.add(fieldSet, centerData);  
	}
	
	//右边显示公告的panel
	private void createCheckPanel(){
		checkPanel = getFormPanel();
		checkPanel.setHeaderVisible(false);

		formBindings = new FormBinding(checkPanel, true);  
		
		Button modify = new Button("修改");
		modify.addSelectionListener(new SelectionListener<ButtonEvent>(){

			@Override
			public void componentSelected(ButtonEvent ce) {
				// TODO Auto-generated method stub
				Notice notice = noticeGridPanel.getGrid().getSelectionModel().getSelectedItem().getBean();
				NoticeWriteWindow nww = new NoticeWriteWindow();
				nww.noticeManagePanel = NoticeManagePanel.this;
				nww.setNotice(notice);
				nww.getTitleFiled().setValue(notice.getNoticeTitle());
				nww.getStartDateFiled().setValue(notice.getStartDate());
				nww.getEndDateFiled().setValue(notice.getEndDate());
				nww.getContent().setValue(notice.getNoticeContent());
				
				nww.recreateWindow();
				
				nww.show();
			}
			
		});
	    Button reset = new Button("重置");
	    checkPanel.addButton(modify);
	    checkPanel.addButton(reset);
	    
	    FieldSet fieldSet = new FieldSet();   
	    fieldSet.setHeading("详情");
	    fieldSet.setLayout(new FitLayout());
	    fieldSet.add(checkPanel);
		BorderLayoutData eastData = new BorderLayoutData(LayoutRegion.EAST,400);
		eastData.setMargins(new Margins(10,10,10,10));
		contentPanel.add(fieldSet, eastData);  
	    
	    
	}
	
	private FormPanel getFormPanel(){
		FormPanel panel = new FormPanel();   
	    panel.setBorders(false);   
	    panel.setBodyBorder(false);   
	    panel.setLabelWidth(55);   
	    panel.setPadding(5);   
	    panel.setHeaderVisible(false);   
	  
	    titleFiled = new TextField<String>();  
	    titleFiled.setFieldLabel("主题"); 
	    titleFiled.setName("noticeTitle");
	    panel.add(titleFiled); 
	     
	    senderFiled = new TextField<String>();  
	    senderFiled.setName("sender");
	    senderFiled.setReadOnly(true);
	    senderFiled.setFieldLabel("发布人");   
	    panel.add(senderFiled);  
	  
	    startDateFiled = new DateField();
	    startDateFiled.setName("startDate");
	    startDateFiled.setFieldLabel("发送时间");   
	    panel.add(startDateFiled);  
	    
	    endDateFiled = new DateField();
	    endDateFiled.setName("endDate");
	    endDateFiled.setFieldLabel("过期时间");   
	    panel.add(endDateFiled);   
	  
	    content = new HtmlEditor(); 
	    content.setName("noticeContent");
	    content.setHideLabel(true);
	    panel.add(content, new FormData("100% -53"));   
	    return panel;
	}


	private void delete() {
		if (grid.getSelectionModel().getSelectedItems().size() == 0) {
			return;
		} else {
			MessageBox.confirm("警告", "您 确定删除该公告（删除后无法恢复）？",
					new Listener<MessageBoxEvent>() {

						@Override
						public void handleEvent(MessageBoxEvent be) {
							// TODO Auto-generated method stub
							Button btn = be.getButtonClicked();
							if (btn.getItemId().equals("yes")) {
								List<PO> list = new ArrayList<PO>();
								for (BeanModel model : grid.getSelectionModel().getSelectedItems()) {
									Notice t = model.getBean();
									list.add(t);
								}
								noticeManagerService.delete(list, new AsyncCallback<Void>() {

											@Override
											public void onFailure(Throwable caught) {
												// TODO Auto-generated
												// method stub
												Info.display("", "失败");
											}

											@Override
											public void onSuccess(Void result) {
												// TODO Auto-generated
												// method stub
												
												Config loadConfig = new Config();
												loadConfig.put("student", App.getUser());
												noticeGridPanel.load(loadConfig);
												grid.getView().refresh(true);
												
												
											}

										});
								

							} else {
								return;
							}
						}
					});
		}
	}

	public BeanGridPanel<Notice> getNoticeGridPanel() {
		return noticeGridPanel;
	}

	public void setNoticeGridPanel(BeanGridPanel<Notice> noticeGridPanel) {
		this.noticeGridPanel = noticeGridPanel;
	}

	
}
